HAL TIM types migration

This section provides information about the HAL TIM types migration .

HAL TIM enumeration migration

HAL TIM state type definition

HAL1

HAL2

typedef enum
{
   HAL_TIM_STATE_RESET

   HAL_TIM_STATE_READY

   HAL_TIM_STATE_BUSY




   HAL_TIM_STATE_TIMEOUT

   HAL_TIM_STATE_ERROR






} HAL_TIM_StateTypeDef;
typedef enum
{
   HAL_TIM_STATE_RESET

   HAL_TIM_STATE_IDLE

   HAL_TIM_STATE_ACTIVE


   HAL_TIM_STATE_INIT






   #if defined(USE_HAL_TIM_DMA) && (USE_HAL_TIM_DMA == 1)
   HAL_TIM_STATE_ACTIVE_SILENT
   #endif /* USE_HAL_TIM_DMA */

} hal_tim_state_t;

Note

HAL2 includes a new state HAL_TIM_ACTIVE_SILENT reflecting the possibility to run the TIM with DMA and without triggering interrupts (silent mode).


HAL TIM channel state type definition

HAL1

HAL2

/**
 * @brief  TIM Channel States definition
 */
typedef enum
{
  HAL_TIM_CHANNEL_STATE_RESET

  HAL_TIM_CHANNEL_STATE_READY


  HAL_TIM_CHANNEL_STATE_BUSY













} HAL_TIM_ChannelStateTypeDef;
/**
 * @brief  TIM Channel States definition
 */
typedef enum
{
  HAL_TIM_CHANNEL_STATE_RESET

  HAL_TIM_OC_CHANNEL_STATE_IDLE
  HAL_TIM_IC_CHANNEL_STATE_IDLE

  HAL_TIM_OC_CHANNEL_STATE_ACTIVE
  HAL_TIM_IC_CHANNEL_STATE_ACTIVE

  #if defined(USE_HAL_TIM_DMA) && (USE_HAL_TIM_DMA == 1)
  /** An internal process is ongoing on the TIM output channel in DMA silent mode */
  HAL_TIM_OC_CHANNEL_STATE_ACTIVE_SILENT
  #endif /* USE_HAL_TIM_DMA */

  #if defined(USE_HAL_TIM_DMA) && (USE_HAL_TIM_DMA == 1)
  /** An internal process is ongoing on the TIM input channel in DMA silent mode */
  HAL_TIM_IC_CHANNEL_STATE_ACTIVE_SILENT   = (HAL_TIM_IC_CHANNEL_STATE_ACTIVE \
                                            | HAL_TIM_ACTIVE_SILENT),
  #endif /* USE_HAL_TIM_DMA */

} hal_tim_channel_state_t;

Note

HAL2 provides a more detailed enumeration, distinguishing between output and input channel states, and includes additional states for DMA silent mode.


HAL TIM active channel type definition

HAL1

HAL2

typedef enum
{
  HAL_TIM_ACTIVE_CHANNEL_1
  HAL_TIM_ACTIVE_CHANNEL_2
  HAL_TIM_ACTIVE_CHANNEL_3
  HAL_TIM_ACTIVE_CHANNEL_4
  HAL_TIM_ACTIVE_CHANNEL_5
  HAL_TIM_ACTIVE_CHANNEL_6
  HAL_TIM_ACTIVE_CHANNEL_CLEARED
} HAL_TIM_ActiveChannel;

Removed . .. note:: In HAL1, the enumeration is utilized to determine the active channel within the callback. In HAL2, the enumeration is removed because it is no longer required, as the channel now becomes an argument in all callbacks related to the channel.


HAL TIM DMA burst states type definition

HAL1

HAL2

typedef enum
{
  HAL_DMA_BURST_STATE_RESET
  HAL_DMA_BURST_STATE_READY
  HAL_DMA_BURST_STATE_BUSY
} HAL_TIM_DMABurstStateTypeDef;

Removed .

Note

In HAL1, the enum is used to define the DMA burst states. In HAL2, the enum is removed as it is no longer necessary.


HAL TIM callback ID type definition

HAL1

HAL2

typedef enum
{
  HAL_TIM_BASE_MSPINIT_CB_ID,
  HAL_TIM_BASE_MSPDEINIT_CB_ID,
  HAL_TIM_IC_MSPINIT_CB_ID,
  HAL_TIM_IC_MSPDEINIT_CB_ID,
  HAL_TIM_OC_MSPINIT_CB_ID,
  HAL_TIM_OC_MSPDEINIT_CB_ID,
  HAL_TIM_PWM_MSPINIT_CB_ID,
  HAL_TIM_PWM_MSPDEINIT_CB_ID,
  HAL_TIM_ONE_PULSE_MSPINIT_CB_ID,
  HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID,
  HAL_TIM_ENCODER_MSPINIT_CB_ID,
  HAL_TIM_ENCODER_MSPDEINIT_CB_ID,
  HAL_TIM_HALL_SENSOR_MSPINIT_CB_ID,
  HAL_TIM_HALL_SENSOR_MSPDEINIT_CB_ID,
  HAL_TIM_PERIOD_ELAPSED_CB_ID,
  HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID,
  HAL_TIM_TRIGGER_CB_ID,
  HAL_TIM_TRIGGER_HALF_CB_ID,
  HAL_TIM_IC_CAPTURE_CB_ID,
  HAL_TIM_IC_CAPTURE_HALF_CB_ID,
  HAL_TIM_OC_DELAY_ELAPSED_CB_ID,
  HAL_TIM_PWM_PULSE_FINISHED_CB_ID,
  HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID,
  HAL_TIM_ERROR_CB_ID,
  HAL_TIM_COMMUTATION_CB_ID,
  HAL_TIM_COMMUTATION_HALF_CB_ID,
  HAL_TIM_BREAK_CB_ID,
  HAL_TIM_BREAK2_CB_ID,
  HAL_TIM_ENCODER_INDEX_CB_ID,
  HAL_TIM_DIRECTION_CHANGE_CB_ID,
  HAL_TIM_INDEX_ERROR_CB_ID,
  HAL_TIM_TRANSITION_ERROR_CB_ID
} HAL_TIM_CallbackIDTypeDef;

Removed .

Note

In HAL2 , a different register callback per event is provided.

Comparative Table for HAL1 and HAL2 TIM Callback Registration

HAL1

HAL2

hal_tim_handle_t tim_handle;

void UpdateCallback(hal_tim_handle_t *htim);
void InputCaptureCallback(hal_tim_handle_t *htim);


/* Register Update Callback */
HAL_TIM_RegisterCallback(&tim_handle,
                         HAL_TIM_PERIOD_ELAPSED_CB_ID,
                         UpdateCallback);

/* Register Input Capture Callback */
HAL_TIM_RegisterCallback(&tim_handle,
                         HAL_TIM_IC_CAPTURE_CB_ID,
                         InputCaptureCallback);

/* UnRegister Update Callback */
HAL_TIM_UnRegisterCallback(&tim_handle,
                           HAL_TIM_PERIOD_ELAPSED_CB_ID);
hal_tim_handle_t tim_handle;

void UpdateCallback(hal_tim_handle_t *htim);
void InputCaptureCallback(hal_tim_handle_t *htim,
                          hal_tim_channel_t channel);

/* Register Update Callback */
HAL_TIM_RegisterUpdateCallback(&tim_handle,
                               UpdateCallback);


/* Register Input Capture Callback */
HAL_TIM_RegisterInputCaptureCallback(&tim_handle,
                                     InputCaptureCallback);


/* UnRegister Update Callback */
HAL_TIM_RegisterUpdateCallback(&tim_handle,
                               HAL_TIM_UpdateCallback);

HAL TIM structure migration

HAL TIM handle type definition

HAL1

HAL2

TIM_HandleTypeDef

hal_tim_handle_t


HAL TIM base initialization type definition

HAL1

HAL2

typedef struct
{
  uint32_t Prescaler;

  uint32_t CounterMode;

  uint32_t Period;

  uint32_t RepetitionCounter;



  uint32_t ClockDivision;

  uint32_t AutoReloadPreload;




} TIM_Base_InitTypeDef;
typedef struct
{
  uint32_t prescaler;

  hal_tim_counter_mode_t counter_mode;

  uint32_t period;

  uint32_t repetition_counter;






  hal_tim_clock_sel_t clock_sel;

} hal_tim_config_t;

Note

In HAL2, the clock selection is added to the TIM configuration structure ( clock_sel). The auto-reload preload setting (HAL1 AutoReloadPreload) and the DTS prescaler (HAL1 ClockDivision) are configured through dedicated HAL2 APIs: HAL_TIM_EnableAutoReloadPreload and HAL_TIM_SetDTSPrescaler.


HAL TIM output compare configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t OCMode;
  uint32_t Pulse;





  uint32_t OCPolarity;
  uint32_t OCNPolarity;

  uint32_t OCIdleState;
  uint32_t OCNIdleState;

  uint32_t OCFastMode;
} TIM_OC_InitTypeDef;
typedef struct
{
  hal_tim_oc_mode_t mode;
  uint32_t pulse;

} hal_tim_oc_compare_unit_config_t;

typedef struct
{
  hal_tim_oc_polarity_t polarity;


  hal_tim_oc_idle_state_t idle_state;

} hal_tim_oc_channel_config_t;

Note

The OCFastMode is an optional configuration, replaced by a unitary function in HAL2, specifically HAL_TIM_OC_EnableChannelFastMode.


HAL TIM input capture configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t ICPolarity;

  uint32_t ICFilter;







  uint32_t ICSelection;

  uint32_t ICPrescaler;

} TIM_IC_InitTypeDef;
typedef struct
{
  hal_tim_ic_polarity_t polarity;

  hal_tim_filter_t filter;

  hal_tim_channel_src_t source;

} hal_tim_ic_channel_config_t;

typedef struct
{
  hal_tim_ic_capture_unit_src_t source;

  hal_tim_ic_capture_unit_prescaler_t prescaler;

} hal_tim_ic_capture_unit_config_t;

HAL TIM encoder configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t EncoderMode;


  uint32_t IC1Polarity;


  uint32_t IC1Selection;


  uint32_t IC1Prescaler;


  uint32_t IC1Filter;


  uint32_t IC2Polarity;


  uint32_t IC2Selection;


  uint32_t IC2Prescaler;


  uint32_t IC2Filter;

} TIM_Encoder_InitTypeDef;

Note

In HAL2, the encoder initialization structure will not exist. It is replaced by the appropriate structure for channel configuration in input mode hal_tim_ic_channel_config_t and hal_tim_ic_capture_unit_config_t and another function for selecting the encoder mode via HAL_TIM_SetConfig or HAL_TIM_SetClockSource.


HAL TIM clock configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t ClockSource;







































  uint32_t ClockPolarity;
  uint32_t ClockPrescaler;
  uint32_t ClockFilter;
} TIM_ClockConfigTypeDef;
typedef struct
{
  hal_tim_clk_src_t clock_source;
  hal_tim_trig_sel_t trigger;
} hal_tim_clock_sel_t;

typedef enum
{
  HAL_TIM_CLK_INTERNAL
  HAL_TIM_CLK_EXTERNAL_MODE1,
  HAL_TIM_CLK_EXTERNAL_MODE2,
  HAL_TIM_CLK_ENCODER_X1_TI1,
  HAL_TIM_CLK_ENCODER_X1_TI2,
  HAL_TIM_CLK_ENCODER_X2_TI1,
  HAL_TIM_CLK_ENCODER_X2_TI2,
  HAL_TIM_CLK_ENCODER_X4_TI12,
  HAL_TIM_CLK_ENCODER_CLK_PLUS_X2,
  HAL_TIM_CLK_ENCODER_CLK_PLUS_X1,
  HAL_TIM_CLK_ENCODER_DIR_CLK_X2,
  HAL_TIM_CLK_ENCODER_DIR_CLK_X1_TI12,
} hal_tim_clk_src_t;

typedef enum
{
  HAL_TIM_TRIG_ITR0,
  HAL_TIM_TRIG_ITR1,
  HAL_TIM_TRIG_ITR2,
  HAL_TIM_TRIG_ITR3,
  HAL_TIM_TRIG_ITR4,
  HAL_TIM_TRIG_ITR5,
  HAL_TIM_TRIG_ITR6,
  HAL_TIM_TRIG_ITR7,
  HAL_TIM_TRIG_ITR8,
#if defined(USB_OTG_FS)
  HAL_TIM_TRIG_ITR11,
#endif /* USB_OTG_FS */
  HAL_TIM_TRIG_TI1F_ED,
  HAL_TIM_TRIG_TI1FP1,
  HAL_TIM_TRIG_TI2FP2,
  HAL_TIM_TRIG_ETRF,
} hal_tim_trig_sel_t;

Note

Based on the nature of the selected clock source, a specific function needs to be called to configure the required parameters (ClockPolarity, ClockPrescaler, and ClockFilter).

  • If the clock source is ETR, call HAL_TIM_SetExternalTriggerInput.

  • For any other clock source, call HAL_TIM_IC_SetConfigChannel.


HAL TIM master configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t MasterOutputTrigger;












  uint32_t MasterOutputTrigger2;

















  uint32_t MasterSlaveMode;

} TIM_MasterConfigTypeDef;
typedef enum
{
  HAL_TIM_TRGO_RESET,
  HAL_TIM_TRGO_ENABLE,
  HAL_TIM_TRGO_UPDATE,
  HAL_TIM_TRGO_CC1IF,
  HAL_TIM_TRGO_OC1,
  HAL_TIM_TRGO_OC2,
  HAL_TIM_TRGO_OC3,
  HAL_TIM_TRGO_OC4,
  HAL_TIM_TRGO_ENCODER_CLK,
} hal_tim_trigger_output_source_t;

typedef enum
{
  HAL_TIM_TRGO2_RESET,
  HAL_TIM_TRGO2_ENABLE,
  HAL_TIM_TRGO2_UPDATE,
  HAL_TIM_TRGO2_CC1F,
  HAL_TIM_TRGO2_OC1,
  HAL_TIM_TRGO2_OC2,
  HAL_TIM_TRGO2_OC3,
  HAL_TIM_TRGO2_OC4,
  HAL_TIM_TRGO2_OC5,
  HAL_TIM_TRGO2_OC6,
  HAL_TIM_TRGO2_OC4_RISING_FALLING,
  HAL_TIM_TRGO2_OC6_RISING_FALLING,
  HAL_TIM_TRGO2_OC4_RISING_OC6_RISING,
  HAL_TIM_TRGO2_OC5_RISING_OC6_RISING,
  HAL_TIM_TRGO2_OC4_RISING_OC6_FALLING,
  HAL_TIM_TRGO2_OC5_RISING_OC6_FALLING,
} hal_tim_trigger_output2_source_t;

Note

The MasterSlaveMode configuration is replaced by a unitary function in HAL2, specifically HAL_TIM_EnableSlaveModePreload.

HAL TIM clear input configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t ClearInputSource;




  uint32_t ClearInputPolarity;

  uint32_t ClearInputPrescaler;

  uint32_t ClearInputFilter;



  uint32_t ClearInputState;



} TIM_ClearInputConfigTypeDef;
typedef enum
{
  HAL_TIM_OCREF_CLR_TIM1_ETR,
  HAL_TIM_OCREF_CLR_TIM1_COMP1_OUT,
  HAL_TIM_OCREF_CLR_TIM1_COMP2_OUT,
            .
            .
            .
            .
} hal_tim_ocref_clr_src_t;

Note

In HAL2, there is no specific structure for ocref clear input configuration. According to the ocref clear source selected, there is a sequence to follow. For example, if ETR is used as the OCREF source, call the function HAL_TIM_SetExternalTriggerInput for the configuration of ClearInputPolarity, ClearInputPrescaler, and ClearInputFilter. The ClearInputState configuration is replaced by a unitary function in HAL2, specifically HAL_TIM_EnableChannelOCRefClear.


HAL TIM one pulse mode configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t OCMode;
  uint32_t Pulse;




  uint32_t OCPolarity;
  uint32_t OCNPolarity;
  uint32_t OCIdleState;
  uint32_t OCNIdleState;





  uint32_t ICPolarity;
  uint32_t ICFilter;




  uint32_t ICSelection;



} TIM_OnePulse_InitTypeDef;
typedef struct
{
  hal_tim_oc_mode_t mode;
  uint32_t pulse;
} hal_tim_oc_compare_unit_config_t;

typedef struct
{
  hal_tim_oc_polarity_t polarity;

  hal_tim_oc_idle_state_t idle_state;

} hal_tim_oc_channel_config_t;

typedef struct
{
  hal_tim_channel_src_t source;
  hal_tim_ic_polarity_t polarity;
  hal_tim_filter_t filter;
} hal_tim_ic_channel_config_t;

typedef struct
{
  hal_tim_ic_capture_unit_src_t source;
  hal_tim_ic_capture_unit_prescaler_t prescaler;
} hal_tim_ic_capture_unit_config_t;

Note

In HAL2, there is no specific structure that directly corresponds to TIM_OnePulse_InitTypeDef from HAL1. The configuration is split into multiple structures.


HAL TIM slave configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t  SlaveMode;
  uint32_t  InputTrigger;


  uint32_t  TriggerPolarity;
  uint32_t  TriggerPrescaler;
  uint32_t  TriggerFilter;
} TIM_SlaveConfigTypeDef;
typedef struct
{
  hal_tim_slave_mode_t mode;
  hal_tim_trig_sel_t trigger;
} hal_tim_slave_config_t;

Note

These parameters (uint32_t TriggerPolarity, uint32_t TriggerPrescaler, uint32_t TriggerFilter) are removed from the structure and their configuration depends on the selected trigger.

  • If Internal Trigger ITRx is selected, there is no configuration to do.

  • If ETRF is selected, the function HAL_TIM_SetExternalTriggerInput is called.

  • If the trigger is TI1F_ED, TI1FP1 or TI2FP2 the functions HAL_TIM_IC_SetConfigChannel and HAL_TIM_IC_SetConfigCaptureUnit are called.


HAL TIM break dead time configuration type definition

HAL1

HAL2

typedef struct
{
  uint32_t OffStateRunMode;
  uint32_t OffStateIDLEMode;




  uint32_t LockLevel;







  uint32_t BreakPolarity;
  uint32_t Break2Polarity;

  uint32_t BreakFilter;
  uint32_t Break2Filter;

  uint32_t BreakAFMode;
  uint32_t Break2AFMode;

  uint32_t DeadTime;

  uint32_t BreakState;
  uint32_t Break2State;

  uint32_t AutomaticOutput;

} TIM_BreakDeadTimeConfigTypeDef;
typedef struct
{
  hal_tim_off_state_run_t off_state_run;
  hal_tim_off_state_idle_t off_state_idle;
} hal_tim_off_states_config_t;

typedef enum
{
  HAL_TIM_LOCK_OFF,
  HAL_TIM_LOCK_1,
  HAL_TIM_LOCK_2,
  HAL_TIM_LOCK_3
} hal_tim_lock_level_t;

typedef struct
{
  hal_tim_break_input_polarity_t polarity;


  hal_tim_filter_t filter;


  hal_tim_break_input_mode_t mode;

} hal_tim_break_input_config_t;

Note

In HAL2, this structure is split into multiple structures or unitary functions for appropriate parameter configuration:

  • uint32_t OffStateRunMode and uint32_t OffStateIDLEMode` are replaced by the function HAL_TIM_BREAK_SetOutputOffStates.

  • uint32_t LockLevel is configured using the function HAL_TIM_SetLockLevel.

  • uint32_t BreakPolarity, uint32_t BreakFilter, uint32_t BreakAFMode, uint32_t Break2Polarity, uint32_t Break2Filter, and uint32_t Break2AFMode are configured using the function HAL_TIM_BREAK_SetConfigInput.

  • uint32_t DeadTime is configured using the function HAL_TIM_SetDeadtime.

  • uint32_t BreakState and uint32_t Break2State are configured using the function HAL_TIM_BREAK_EnableInput.

  • uint32_t AutomaticOutput is configured using the function HAL_TIM_BREAK_EnableAutomaticOutput.


HAL TIM callback type definition

HAL1

HAL2

pTIM_CallbackTypeDef

hal_tim_cb_t

Related concept: Replaced TypeDef suffix with _t in type names


HAL TIM hall sensor configuration type definition

HAL1

HAL2

typedef struct
{


  uint32_t IC1Polarity;
  uint32_t IC1Filter;




  uint32_t IC1Prescaler;





  uint32_t Commutation_Delay;

} TIM_HallSensor_InitTypeDef;
typedef struct
{
  hal_tim_channel_src_t source;

  hal_tim_ic_polarity_t polarity;
  hal_tim_filter_t filter;
} hal_tim_ic_channel_config_t;

typedef struct
{
  hal_tim_ic_capture_unit_prescaler_t prescaler;
  hal_tim_ic_capture_unit_src_t source;
} hal_tim_ic_capture_unit_config_t;

typedef struct
{
  uint32_t pulse;


  hal_tim_oc_mode_t mode;
} hal_tim_oc_compare_unit_config_t;

Note

In HAL2, this structure is split into multiple structures for appropriate parameter configuration:

  • uint32_t IC1Polarity and uint32_t IC1Filter are configured using the hal_tim_ic_channel_config_t structure.

  • uint32_t IC1Prescaler is configured using the hal_tim_ic_capture_unit_config_t structure.

  • uint32_t Commutation_Delay is configured using the hal_tim_oc_compare_unit_config_t structure.


HAL TIM encoder index configuration type definition

HAL1

HAL2

typedef struct
{


  uint32_t Polarity;
  uint32_t Prescaler;
  uint32_t Filter;





  uint32_t Blanking;
  FunctionalState FirstIndexEnable;
  uint32_t Position;
  uint32_t Direction;

} TIMEx_EncoderIndexConfigTypeDef;
typedef struct
{
  hal_tim_ext_trig_src_t source;

  hal_tim_ext_trig_polarity_t polarity;
  hal_tim_ext_trig_prescaler_t prescaler;
  hal_tim_filter_t filter;

} hal_tim_ext_trig_config_t;

typedef struct
{
  hal_tim_encoder_index_blank_mode_t blanking;
  hal_tim_encoder_index_sel_t idx;
  hal_tim_encoder_index_pos_sel_t pos;
  hal_tim_encoder_index_dir_t dir;


} hal_tim_encoder_index_config_t;

Note

In HAL2, this structure is split into multiple structures for appropriate parameter configuration:

  • uint32_t Polarity, uint32_t Prescaler, and uint32_t Filter are configured using the hal_tim_ext_trig_config_t structure.

  • uint32_t Blanking, uint32_t Position, uint32_t Direction and FunctionalState FirstIndexEnable are configured using the hal_tim_encoder_index_config_t structure.


HAL TIM break input configuration type definition

HAL1

HAL2

typedef struct
{

  uint32_t Polarity;




  uint32_t Source;

  uint32_t Enable;

} TIMEx_BreakInputConfigTypeDef;
typedef enum
{
  HAL_TIM_BREAK_INPUT_SRC_NONINVERTED,
  HAL_TIM_BREAK_INPUT_SRC_INVERTED
} hal_tim_break_input_src_polarity_t;

Note

In HAL2, this structure is removed and replaced by a constants macro, an enum, and unitary functions for appropriate parameter configuration:

  • uint32_t Source is removed and replaced by constants macro:

    • #define HAL_TIM_BREAK_INPUT_GPIO

    • #define HAL_TIM_BREAK_INPUT_COMP1

    • … other constants

  • uint32_t Enable is replaced by the function HAL_TIM_BREAK_EnableInputSource.

  • uint32_t Polarity is replaced by the enum hal_tim_break_input_src_polarity_t.